package example.runner;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

/**
 * GUI: von den Tanks (Sheepy) weitgehend übernommen.
 */
public class ControlField extends javax.swing.JFrame
{
	private static final long serialVersionUID = -1389478513013808908L;
	private JPanel jSpielfeld;
	private Game _game;

	/**
	 * Startet das Programm
	 */
	public static void main(String[] args)
	{
		new ControlField();
	}

	/**
	 * Erstellt das Fenster, zeigt es an und lässt das Spiel beginnen.
	 */
	public ControlField()
	{
		super();
		System.out.println("ge");
		_game = new Game(this);
		initGUI();
		this.setVisible(true);
		_game.startGame();
	}

	private void initGUI()
	{
		try
		{
			this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setBounds(450, 0, 900, 850);
			{
				jSpielfeld = new PrivatePanel(_game);
				getContentPane().add(jSpielfeld, "Center");
				jSpielfeld.setBounds(10, 10, 800, 800);
				jSpielfeld.setLayout(null);
				jSpielfeld.setBorder(new LineBorder(
						new java.awt.Color(0, 0, 0), 1, false));
			}
			{
				JButton b = new JButton("Speed");
				b.setVisible(true);
				b.setBounds(jSpielfeld.getWidth() + 40, 30, 100, 40);
				b.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						setSpeed();
					}
				});
				getContentPane().add(b);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		System.out.println("init");
	}


	protected void setSpeed() {
		int result = Integer.parseInt(JOptionPane.showInputDialog(null));
		_game.setSpeedVerz(result);
		
	}// TODO Auto-generated method stub
	


	private static class PrivatePanel extends JPanel
	{
		private static final long serialVersionUID = 2444492945961210136L;
		private Game _game;

		public PrivatePanel(Game game)
		{
			super();
			_game = game;
		}

		public void paint(Graphics g)
		{
			FieldValues[][] field = _game.getField();
			if (field != null)
			{
				this.setSize(800, 800);
				for (int x = 0; x < Game.FIELD_WIDTH; x++)
				{
					for (int y = 0; y < Game.FIELD_HEIGHT; y++)
					{
						if (field[x][y] == FieldValues.Frei)
						{
							g.setColor(new Color(200, 200, 200));
						}
						else if (field[x][y] == FieldValues.Wand)
						{
							g.setColor(new Color(125, 125, 0));
						}
						else if (field[x][y] == FieldValues.Essen)
						{
							g.setColor(new Color(0, 200, 200));
						}
						else
						{
							g.setColor(new Color(200, 0, 0));
						}
						g.fillRect(x * 800/Game.FIELD_WIDTH, y * 800/Game.FIELD_HEIGHT, 800/Game.FIELD_WIDTH, 800/Game.FIELD_HEIGHT);

					}
				}

			}

		}
	}

	public void close()
	{
		this.setVisible(false);
		jSpielfeld = null;
		this.dispose();
	}
}
